﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Drawing2D; 

namespace TS_XY_Train_Repaire.SysSetting
{
    public partial class SystemSettingForm : DevExpress.XtraEditors.XtraForm
    {
        public SystemSettingForm()
        {
            InitializeComponent();
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                SaveSetting();
            }
            catch
            {
                MessageBox.Show("参数保存错误！");
            }
        }
        public void SaveSetting()
        {
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(TS_Win_Class._xml_SysSetting_Filename); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//LocalSetting//SaveAccount");//加载是否保存账号节点
                if (cb_Setting_SaveAccount.Checked)
                {
                    xmlNode.InnerText = "1";
                }
                else
                {
                    xmlNode.InnerText = "0";
                }
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//LocalSetting//AutoLogin");//加载是否自动登录节点
                if (cb_Setting_AutoLogin.Checked)
                {
                    xmlNode.InnerText = "1";
                }
                else
                {
                    xmlNode.InnerText = "0";
                }
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//LocalSetting//AutoTracNet");//加载是否自动侦测网络节点
                if (cb_Setting_AutoNet.Checked)
                {
                    xmlNode.InnerText = "1";
                }
                else
                {
                    xmlNode.InnerText = "0";
                }
                GetLocalSetting.GetLocalSetting gl = new GetLocalSetting.GetLocalSetting();
                gl.set_Net_AutoDetect(cb_Setting_AutoNet.Checked);
                GetLocalSetting.GetLocalSetting.SetLocalSetting(gl);

                xmlDoc.Save(TS_Win_Class._xml_SysSetting_Filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                tssl_Setting.Text = "本地参数改动已保存！";
            }
        }

        private void bt_SQL_Test_Click(object sender, EventArgs e)
        {
            lb_Server_List.Items.Clear();
            if (tb_Server_IP.Text.Trim().Length > 0 && tb_Server_US.Text.Trim().Length > 0 && tb_Server_PS.Text.Trim().Length > 0)
            {
                tssl_Setting.Text = "正在连接服务器... ...请稍后！";
                SqlConnection conn = new SqlConnection("server=" + tb_Server_IP.Text.Trim() + "," + tb_Server_Port.Text.Trim() + ";uid=" + tb_Server_US.Text.Trim() + ";pwd=" + tb_Server_PS.Text.Trim());
                SqlCommand cmd = new SqlCommand("select name from sys.databases", conn);
                try
                {
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        lb_Server_List.Items.Add(dr[0]);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    tssl_Setting.Text = "服务器连接错误！";
                }
                finally
                {
                    conn.Close();
                    tssl_Setting.Text = "服务器连接成功！";
                }
            }
            else
            {
                MessageBox.Show("服务器地址、用户和密码不能为空,请填写完整！");
                tssl_Setting.Text = "服务器地址、用户和密码不能为空,请填写完整！";
            }
        }

        private void SystemSettingForm_Load(object sender, EventArgs e)
        {
            foreach (DevExpress.Skins.SkinContainer skin in DevExpress.Skins.SkinManager.Default.Skins)
                cmbAppStyle.Properties.Items.Add(skin.SkinName);
            tssl_Setting.Text = "系统设置";
            LoadSysSetting();
            if (TS_Win_Locomotive_MainForm.pUserInfoMain.UserLoginState)
            {
                cb_DataRank_RB.Text = TS_Win_Locomotive_MainForm.pUserInfoMain.UserRBName;
                cb_DataRank_RS.Text = TS_Win_Locomotive_MainForm.pUserInfoMain.UserRSName;
                cb_DataRank_DP.Text = TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepName;
                switch (TS_Win_Locomotive_MainForm.pUserInfoMain.UserDataRankID)
                {
                    case "0":
                        cb_DataRank_SA.Checked = true;
                        cb_DataRank_RB.Checked = true;
                        cb_DataRank_RS.Checked = true;
                        cb_DataRank_DP.Checked = true;
                        cb_DataRank_Null.Checked = true;
                        break;
                    case "1":
                        cb_DataRank_SA.Checked = false;
                        cb_DataRank_RB.Checked = true;
                        cb_DataRank_RS.Checked = true;
                        cb_DataRank_DP.Checked = true;
                        cb_DataRank_Null.Checked = true;
                        break;
                    case "2":
                        cb_DataRank_SA.Checked = false;
                        cb_DataRank_RB.Checked = false;
                        cb_DataRank_RS.Checked = true;
                        cb_DataRank_DP.Checked = true;
                        cb_DataRank_Null.Checked = true;
                        break;
                    case "3":
                        cb_DataRank_SA.Checked = false;
                        cb_DataRank_RB.Checked = false;
                        cb_DataRank_RS.Checked = false;
                        cb_DataRank_DP.Checked = true;
                        cb_DataRank_Null.Checked = true;
                        break;
                    default:
                        cb_DataRank_SA.Checked = false;
                        cb_DataRank_RB.Checked = false;
                        cb_DataRank_RS.Checked = false;
                        cb_DataRank_DP.Checked = false;
                        cb_DataRank_Null.Checked = true;
                        break;
                }
            }
            else
            {
                cb_DataRank_RB.Text = "局";
                cb_DataRank_RB.Checked = false;
                cb_DataRank_RS.Text = "站段";
                cb_DataRank_RS.Checked = false;
                cb_DataRank_DP.Text = "部门";
                cb_DataRank_DP.Checked = false;
                cb_DataRank_Null.Checked = true;
                cb_DataRank_SA.Checked = false;
            }
        }

        private void lb_Server_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb_Server_List.SelectedIndex > -1)
            {
                tssl_Setting.Text = "你选择的服务器是" + lb_Server_List.Text.Trim();
                bt_SQL_Save.Enabled = true;
            }
        }

        private void SystemSettingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void LoadSysSetting()
        {
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(TS_Win_Class._xml_SysSetting_Filename); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer");//加载SQLServer节点
                tb_Server_IP.Text = DESEncrypt.Decrypt(xmlNode.ChildNodes.Item(0).InnerText,TS_Win_Class._xml_SysSetting_CompanyName);
                tb_Server_Port.Text = DESEncrypt.Decrypt(xmlNode.ChildNodes.Item(1).InnerText, TS_Win_Class._xml_SysSetting_CompanyName);
                tb_Server_US.Text = DESEncrypt.Decrypt(xmlNode.ChildNodes.Item(3).InnerText, TS_Win_Class._xml_SysSetting_CompanyName);
                tb_Server_PS.Text = DESEncrypt.Decrypt(xmlNode.ChildNodes.Item(4).InnerText, TS_Win_Class._xml_SysSetting_CompanyName);

                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//LocalSetting");
                if (xmlNode.ChildNodes.Item(0).InnerText == "0")
                {
                    cb_Setting_SaveAccount.Checked = false;
                }
                else
                {
                    cb_Setting_SaveAccount.Checked = true;
                }
                if (xmlNode.ChildNodes.Item(1).InnerText == "0")
                {
                    cb_Setting_AutoLogin.Checked = false;
                }
                else
                {
                    cb_Setting_AutoLogin.Checked = true;
                }
                if (xmlNode.ChildNodes.Item(2).InnerText == "0")
                {
                    cb_Setting_AutoNet.Checked = false;
                }
                else
                {
                    cb_Setting_AutoNet.Checked = true;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                //throw ex; //这里可以定义你自己的异常处理
            }
        }

        private void bt_SQL_Save_Click(object sender, EventArgs e)
        {
            bt_SQL_Save.Enabled = false;
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(TS_Win_Class._xml_SysSetting_Filename); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer//ServerName");//加载SQL服务器IP节点
                xmlNode.InnerText = DESEncrypt.Encrypt(tb_Server_IP.Text.Trim(), TS_Win_Class._xml_SysSetting_CompanyName);
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer//ServerPort");//加载SQL服务器端口节点
                xmlNode.InnerText = DESEncrypt.Encrypt(tb_Server_Port.Text.Trim(), TS_Win_Class._xml_SysSetting_CompanyName);
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer//Database");//加载SQL服务器数据库名节点
                xmlNode.InnerText = DESEncrypt.Encrypt(lb_Server_List.Text.Trim(), TS_Win_Class._xml_SysSetting_CompanyName);
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer//Account");//加载SQL服务器用户节点
                xmlNode.InnerText = DESEncrypt.Encrypt(tb_Server_US.Text.Trim(), TS_Win_Class._xml_SysSetting_CompanyName);
                xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//SQLServer//Password");//加载SQL服务器密码节点
                xmlNode.InnerText = DESEncrypt.Encrypt(tb_Server_PS.Text.Trim(), TS_Win_Class._xml_SysSetting_CompanyName);
                xmlDoc.Save(TS_Win_Class._xml_SysSetting_Filename);
                GetSQLConnString.GetSQLConnString gs = new GetSQLConnString.GetSQLConnString();
                gs.setConnnString(@"Data Source=" + tb_Server_IP.Text.Trim() + "," + tb_Server_Port.Text.Trim() + ";Initial Catalog=" + lb_Server_List.Text.Trim() + ";password=" + tb_Server_PS.Text.Trim() + ";Pooling=false;persist security info=True;user id=" + tb_Server_US.Text.Trim() + ";workstation id=SERVER;packet size=4096");
                GetSQLConnString.GetSQLConnString.SetSQLConnString(gs);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                tssl_Setting.Text = "本地参数改动已保存！";
            }
        }

        private void bt_Clear_Click(object sender, EventArgs e)
        {
        }
        private void cmbAppStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(TS_Win_Class._xml_SysSetting_Filename); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode("//xml_TS_AllInOne//LocalSetting//SkinName");//
                xmlNode.InnerText = cmbAppStyle.EditValue.ToString();
                xmlDoc.Save(TS_Win_Class._xml_SysSetting_Filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                tssl_Setting.Text = "本地参数改动已保存！";
            }
        }
    }
}
